from .id_generator import generate_id

def create_item(items, name, item_type, level):
    """创建新项目并添加到集合中"""
    if len(items) >= 4:
        print("❌ 错误：最多只能有4个项目！")
        return items
    
    if not name or not item_type:
        print("❌ 错误：名称和类型不能为空！")
        return items
    
    try:
        level = int(level)
        if level < 1 or level > 100:
            print("❌ 错误：等级必须在1-100之间！")
            return items
    except ValueError:
        print("❌ 错误：等级必须是数字！")
        return items
    
    new_item = {
        "id": generate_id(),
        "name": name.strip(),
        "type": item_type.strip(),
        "level": level
    }
    
    items.append(new_item)
    print(f"✅ {name} 添加成功！ID: {new_item['id'][:6]}...")
    return items

def delete_item(items, item_id):
    """根据ID删除项目"""
    for item in items:
        if item["id"] == item_id or item["id"].startswith(item_id):
            items.remove(item)
            print(f"✅ {item['name']} 删除成功！")
            return items
    
    print("❌ 错误：未找到该项目！")
    return items

def get_item_by_id(items, item_id):
    """根据ID获取项目"""
    for item in items:
        if item["id"] == item_id or item["id"].startswith(item_id):
            return item
    return None

def get_items_count(items):
    """获取当前项目数量"""
    return len(items)

def can_add_more_items(items):
    """检查是否可以添加更多项目"""
    return len(items) < 4